<%
# encoding: utf-8
#
# Redmine plugin for Document Management System "Features"
#
# Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<% all_assignments =  DmsfWorkflowStepAssignment.
      left_outer_joins(:dmsf_workflow_step_actions).
      joins(:dmsf_workflow_step).
      joins(:dmsf_file_revision).
      where(dmsf_file_revisions: { deleted: DmsfFileRevision::STATUS_ACTIVE, workflow: DmsfWorkflow::STATE_WAITING_FOR_APPROVAL }).
      where(dmsf_workflow_step_assignments: { user_id: @user&.id }).
      where(['dmsf_workflow_step_actions.id IS NULL OR dmsf_workflow_step_actions.action = ?',
       DmsfWorkflowStepAction::ACTION_DELEGATE]).
      order('dmsf_workflow_step_assignments.dmsf_file_revision_id, dmsf_workflow_steps.step').to_a %>
<% assignments = all_assignments.delete_if { |a| (a.dmsf_file_revision != a.dmsf_file_revision.dmsf_file.last_revision) } %>
<% assignments.uniq! { |a| a.dmsf_file_revision } %>
<% easy_extension = defined?(EasyExtensions) %>
<% unless easy_extension %>
  <h3><%= l(:open_approvals)%> (<%= assignments.size %>)</h3>
<% end %>
<% if assignments.any? %>
  <%= form_tag({}, data: { cm_url: dmsf_context_menu_path }) do %>
    <%= hidden_field_tag 'back_url', my_page_path, id: nil %>
    <table class="list">
      <thead>
        <tr>
          <% unless easy_extension %>
            <th class="checkbox hide-when-print">
              <%= check_box_tag 'check_all', '', false, class: 'toggle-selection',
                                title: "#{l(:button_check_all)}/#{l(:button_uncheck_all)}" %>
            </th>
          <% end %>
          <th><%= l(:field_project) %></th>
          <th><%= l(:field_label_dmsf_workflow) %></th>
          <th><%= l(:field_status) %></th>
          <th><%= l(:label_document) %></th>
          <th><%= l(:field_folder) %></th>
          <% unless easy_extension %>
            <th></th>
          <% end %>
        </tr>
      </thead>
      <tbody>
        <% assignments.each do |assignment| %>
          <tr id="file-<%= assignment.dmsf_file_revision.dmsf_file.id %>" class="hascontextmenu">
            <% unless easy_extension %>
              <td class="checkbox hide-when-print">
                <%= check_box_tag 'ids[]', "file-#{assignment.dmsf_file_revision.dmsf_file.id}", false, id: nil %>
              </td>
            <% end %>
            <td class="project">
              <% if assignment.dmsf_file_revision.dmsf_file.project %>
                <%= link_to_project assignment.dmsf_file_revision.dmsf_file.project %>
              <% end %>
            </td>
            <td>
              <% if assignment.dmsf_workflow_step && assignment.dmsf_workflow_step.dmsf_workflow %>
                  <%= link_to h(assignment.dmsf_workflow_step.dmsf_workflow.name),
                    edit_dmsf_workflow_path(assignment.dmsf_workflow_step.dmsf_workflow) %>
              <% end %>
            </td>
            <td class="dmsf_workflow">
              <% if assignment.dmsf_workflow_step.dmsf_workflow %>
                  <%= link_to assignment.dmsf_file_revision.workflow_str(false),
                          log_dmsf_workflow_path(
                              project_id: assignment.dmsf_file_revision.dmsf_file.project_id,
                              id: assignment.dmsf_workflow_step.dmsf_workflow_id,
                              dmsf_file_revision_id: assignment.dmsf_file_revision_id),
                          title: assignment.dmsf_file_revision.workflow_tooltip,
                          remote: true %>
              <% else %>
                  <%= assignment.dmsf_file_revision.workflow_str(false) %>
              <% end %>
            </td>
            <td class="title">
              <% if assignment.dmsf_file_revision && assignment.dmsf_file_revision.dmsf_file %>
                <%= link_to h(assignment.dmsf_file_revision.title),
                            dmsf_file_path(id: assignment.dmsf_file_revision.dmsf_file) %>
              <% end %>
            </td>
            <td class="title">
              <% if assignment.dmsf_file_revision %>
                <% if assignment.dmsf_file_revision.dmsf_file.dmsf_folder %>
                  <%= link_to h(assignment.dmsf_file_revision.dmsf_file.dmsf_folder.title),
                        dmsf_folder_path(id: assignment.dmsf_file_revision.dmsf_file.project,
                        folder_id: assignment.dmsf_file_revision.dmsf_file.dmsf_folder) %>
                <% elsif assignment.dmsf_file_revision.dmsf_file.project %>
                  <%= link_to l(:link_documents), dmsf_folder_path(id: assignment.dmsf_file_revision.dmsf_file.project) %>
                <% end %>
              <% end %>
            </td>
            <% unless easy_extension %>
              <td class="buttons">
                <%= link_to_context_menu %>
              </td>
            <% end %>
          </tr>
        <% end %>
      </tbody>
    </table>
  <% end %>
  <%= context_menu unless easy_extension %>
<% else %>
  <p class="nodata"><%= l(:label_no_data) %></p>
<% end %>
